package com.edu.service.impl;

import com.edu.commons.JWTUtil;
import com.edu.commons.MD5Utils;
import com.edu.commons.ServiceCode;
import com.edu.commons.ServiceException;
import com.edu.dto.UpdatePwdDTO;
import com.edu.dto.UserLoginDTO;
import com.edu.dto.UserSignDTO;
import com.edu.mapper.UserMapper;
import com.edu.pojo.User;
import com.edu.service.UserService;
import com.edu.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ProjectName: music-project
 * @Titile: UserServiceImpl
 * @Author: Administrator
 * @Description: 用户业务实现类
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> findAll() {
        List<User> users = userMapper.findAll();
        if(users!=null&& !users.isEmpty()){
            return users;
        }
        throw new RuntimeException("没有数据");
    }

    @Override
    public UserVO login(UserLoginDTO userLoginDTO) {
        // 如果校验框架不能校验的数据，我们可以在这里校验
        User user = userMapper.findByAccount(userLoginDTO.getAccount());
        if(user==null){
            throw new ServiceException(ServiceCode.ERROR_DATA_NOT_FOUND,"用户未注册");
        }

        //将登录密码加密
        String pwd = MD5Utils.stringToMD5(userLoginDTO.getPassword());
        //跟数据库中的密码进行比对密码
        if(!pwd.equals(user.getPassword())){
            throw new ServiceException(ServiceCode.ERROR_DATA_NOT_FOUND,"密码错误");
        }
        UserVO userVO = new UserVO();
        userVO.setUsername(user.getUsername());
        userVO.setImageUrl(user.getImageUrl());
        //生成token
        String token = JWTUtil.generateToken(user.getId(), user.getUsername());
        userVO.setToken(token);
        return userVO;
    }
    @Override
    public UserVO sign(UserSignDTO userSignDTO) {
        Date date = new Date();
        date.setTime(date.getTime() + 1000 * 60 * 60);
        User user = userMapper.findByAccount(userSignDTO.getUsername());
        User user1= userMapper.findByAccount(userSignDTO.getPhone());
        User user2 = userMapper.findByAccount(userSignDTO.getEmail());

        if(user != null){
            throw new ServiceException(ServiceCode.ERROR_DATA_NOT_FOUND,"用户名已存在");
        }

        if(user1 != null){
            throw new ServiceException(ServiceCode.ERROR_DATA_NOT_FOUND,"电话号码已注册已存在");
        }

        if(user2 != null){
            throw new ServiceException(ServiceCode.ERROR_DATA_NOT_FOUND,"邮箱已注册");
        }

        if (!userSignDTO.getPassword().equals(userSignDTO.getConfirmpassword())) {
            throw new ServiceException(ServiceCode.ERROR_DATA_NOT_FOUND,"两次输入密码不一致");
        }

        String pwd = MD5Utils.stringToMD5(userSignDTO.getPassword());
        user = new User();
        user.setUsername(userSignDTO.getUsername());
        user.setPassword(pwd);
        user.setCreateTime(date);
        user.setPhone(userSignDTO.getPhone());
        user.setEmail(userSignDTO.getEmail());

//        user.setId(user.getId());
        if (userSignDTO.getRole() == 1) {
            user.setRole(1);

        } else {
            user.setRole(2);
        }
        userMapper.CreateUser(user);
        UserVO userVO = new UserVO();
        userVO.setUsername(userSignDTO.getUsername());

        User u =  userMapper.findByAccount(userSignDTO.getPhone());
        String token = JWTUtil.generateToken(u.getId(), u.getUsername());
        userVO.setToken(token);
        return userVO;
    }

    @Override
    public void updatepwd(UpdatePwdDTO updatePwdDTO) {
        User user = userMapper.findByAccount(updatePwdDTO.getAccount());
        if (user == null) {
            throw new ServiceException(ServiceCode.ERROR_DATA_NOT_FOUND,"用户不存在");
        }
        if (!updatePwdDTO.getPassword().equals(updatePwdDTO.getConfirmpassword())) {
            throw new ServiceException(ServiceCode.ERROR_DATA_NOT_FOUND,"两次输入密码不一致");
        }
        String pwd = MD5Utils.stringToMD5(updatePwdDTO.getPassword());
        userMapper.UpdatePwd(updatePwdDTO.getAccount(), pwd);
    }
}
